package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.zhentao.api.article.ArticleClientApi;
import com.zhentao.mapper.WmChannelMapper;
import com.zhentao.mapper.WmNewsMapper;
import com.zhentao.mapper.WmUserMapper;
import com.zhentao.model.article.dto.ArticleDto;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.wemedia.pojo.WmChannel;
import com.zhentao.model.wemedia.pojo.WmNews;
import com.zhentao.model.wemedia.pojo.WmUser;
import com.zhentao.service.WmNewsAutoScanService;
import com.zhentao.utils.aliyun.TextModerationPlusUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class WmNewsAutoScanServiceImpl implements WmNewsAutoScanService {
    @Autowired
    private WmNewsMapper wmNewsMapper;
    @Autowired
    private ArticleClientApi articleClientApi;
    @Autowired
    private WmChannelMapper channelMapper;
    @Autowired
    private WmUserMapper wmUserMapper;

    @Override
    public void autoScanWnMews(Integer id) {
        WmNews wmNews = wmNewsMapper.selectById(id);
        if (wmNews != null) {
            //获取内容
            String title = wmNews.getTitle();
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(title);
            List<Map> maps = JSON.parseArray(wmNews.getContent(), Map.class);
            for (Map map : maps) {
                if (map.get("type").equals("text")) {
                    //传递值images
                    stringBuffer.append((String) map.get("value"));
                }
            }
            ResponseResult responseResult = TextModerationPlusUtil.checkContent(stringBuffer.toString());
            if (responseResult.getCode() == 600) {
                //审核通过
                wmNews.setStatus((short) 8);
                wmNewsMapper.updateById(wmNews);
                //审核通过后同步信息
                ArticleDto articleDto = new ArticleDto();
                BeanUtils.copyProperties(wmNews, articleDto);
                //需不需要补充参数
                //频道名字
                WmChannel wmChannel = channelMapper.selectById(wmNews.getChannelId());
                if (wmChannel != null) {
                    articleDto.setChannelName(wmChannel.getName());
                }
                //作者id和作者名字 -根据作者的id查询出作者的名字
                WmUser wmUser = wmUserMapper.selectById(wmNews.getUserId());
                articleDto.setAuthorName(wmUser.getName());
                articleDto.setAuthorId(wmNews.getUserId().longValue());
                articleDto.setLayout(wmNews.getType());
                //完成同步操作
                ResponseResult result = articleClientApi.saveArtice(articleDto);
                if (result.getCode() == 200) {
                    //同步成功
                    //进行关联
                    wmNews.setArticleId((Long) result.getData());
                    //是否发布
                    Date date = wmNews.getPublishTime();
                    if (!date.after(new Date())) {
                        //发布成功 状态改为9
                        wmNews.setStatus((short) 9);
                    }
                    wmNewsMapper.updateById(wmNews);
                }
            } else if (responseResult.getCode() == 602) {
                //转为人工审核
                wmNews.setStatus((short) 3);
                wmNewsMapper.updateById(wmNews);
            } else {
                //审核未通过，被拒绝
                wmNews.setStatus((short) 2);
                wmNews.setReason(responseResult.getErrorMessage());
                wmNewsMapper.updateById(wmNews);
            }
        }
    }

    @Override
    public ResponseResult saveAppArticle(WmNews wmNews) {
        ArticleDto dto = new ArticleDto();
        //属性的拷贝
        BeanUtils.copyProperties(wmNews, dto);
        //文章的布局
        dto.setLayout(wmNews.getType());
        //频道
        WmChannel wmChannel = channelMapper.selectById(wmNews.getChannelId());
        if (wmChannel != null) {
            dto.setChannelName(wmChannel.getName());
        }

        //作者
        dto.setAuthorId(wmNews.getUserId().longValue());
        WmUser wmUser = wmUserMapper.selectById(wmNews.getUserId());
        if (wmUser != null) {
            dto.setAuthorName(wmUser.getName());
        }

        //设置文章id
        if (wmNews.getArticleId() != null) {
            dto.setId(wmNews.getArticleId());
        }
        dto.setCreatedTime(new Date());

        ResponseResult responseResult = articleClientApi.saveArtice(dto);
        return responseResult;
    }
}
